package com.github.ilioili.justdoit.components.fragment;

import android.graphics.Color;
import android.graphics.Paint;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.config.AppConfig;
import com.github.ilioili.justdoit.common.config.EventConstants;
import com.github.ilioili.justdoit.components.activity.CreateOrModifyActivity;
import com.github.ilioili.justdoit.components.activity.DetailActivity;
import com.github.ilioili.justdoit.components.activity.MainActivity;
import com.github.ilioili.justdoit.components.activity.PickDateActivity;
import com.github.ilioili.justdoit.components.activity.PickTimeActivity;
import com.github.ilioili.justdoit.model.dao.RecordDao;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.model.pojo.EventRecord;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.components.receiver.AlarmReceiver;
import com.github.ilioili.justdoit.util.CompatUtil;
import com.github.ilioili.justdoit.util.DateUtil;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.github.ilioili.justdoit.widget.SatelliteView;
import com.github.ilioili.justdoit.widget.SlideListItemWrapper;
import com.github.ilioili.justdoit.widget.WatchTimePicker;
import com.taihe.template.base.BaseActivity;
import com.taihe.template.base.BaseFragment;
import com.taihe.template.base.injection.Click;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.InjectionUtil;
import com.taihe.template.base.injection.Layout;
import com.taihe.template.base.util.SizeUtil;
import com.taihe.template.base.util.ToastUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by hantuo on 16/5/15.
 */
@Layout(R.layout.fragment_daily_to_day_list)
public class ToDoListOfTodayFragment extends BaseFragment {

    @Id(R.id.tv_complete)
    private TextView tvComplete;
    @Id(R.id.recyclerView)
    private RecyclerView recyclerView;
    @Id(R.id.num_complete)
    private TextView tvNumComplete;
    @Id(R.id.num_all)
    private TextView tvNumAll;
    @Id(R.id.num_uncomplete)
    private TextView tvNumUncomplete;
    @Id(R.id.tv_time_spent)
    private TextView tvTimeSpent;

    private List<ScheduleEvent> dataList = new ArrayList<>();
    private List<EventRecord> recordsOfToday = new ArrayList<>();
    private SatelliteMenu satelliteMenu;

    private SlideListItemWrapper slideListItemWrapper;

    @Override
    protected void init(View rootView) {
        super.init(rootView);
        if (!AppConfig.isSlideDeleteConfirmed()) {
            recyclerView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Snackbar snackbar = Snackbar.make(recyclerView, "完成条目请向右滑动条目\n更多操作请向左滑动条目", 5000);
                    snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AppConfig.setSlideDeleteConfirmed(true);
                        }
                    });
                    snackbar.show();
                }
            }, 2000);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    closeOpendItemIfExit();
                }
                return false;
            }
        });
        recyclerView.setAdapter(new RecyclerView.Adapter<Holder>() {

            private int completeColor = CompatUtil.getColor(getContext(), R.color.font_light_gray);
            private int uncompleteColor = CompatUtil.getColor(getContext(), R.color.font_normal);

            @Override
            public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = null;
                if (viewType == 0) {
                    view = LayoutInflater.from(getActivity()).inflate(R.layout.daily_routuine_to_do_item, parent, false);
                } else {
                    view = new View(getContext());
                    view.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtil.dpToPx(100, getContext())));
                }
                return new Holder(view, viewType);
            }

            @Override
            public void onBindViewHolder(Holder holder, int position) {
                if (holder.type != 0) return;

                ScheduleEvent dataItem = dataList.get(position);

//                holder.vFirstView.setBackgroundColor(dataItem.color);
                holder.vFirstView.setTag(dataItem);
                holder.itemView.setTag(dataItem);
                holder.tvName.setTag(dataItem);
                holder.btnDelete.setTag(dataItem);
                holder.btnAlarm.setTag(dataItem);
                holder.btnUndo.setTag(dataItem);
                holder.btnArrange.setTag(dataItem);
                holder.btnEditRoutine.setTag(dataItem);
//                if (dataItem.type == EventConstants.TYPE_EVENT) {
//                    holder.ivType.setImageResource(R.drawable.ic_calendar_today_24dp);
//                } else if (dataItem.type == EventConstants.TYPE_TARGET) {
//                    holder.ivType.setImageResource(R.drawable.ic_target_24dp);
//                } else {
//                    holder.ivType.setImageResource(R.drawable.ic_repeat_24dp);
//                }

                if (isComplete(dataItem)) {
                    holder.tvName.setText(dataItem.name);
                    holder.tvName.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                    holder.tvName.setTextColor(completeColor);
//                    holder.tvSub.setTextColor(CompatUtil.getColor(getContext(), R.color.font_gray));
                    holder.ivType.setColorFilter(completeColor);
                    setGone(holder.btnAlarm, holder.btnArrange, holder.btnDelete, holder.btnEditRoutine);
                    setVisiable(holder.btnUndo);
                } else {
                    holder.tvName.getPaint().setFlags(Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                    setVisiable(holder.btnAlarm, holder.btnArrange, holder.btnDelete, holder.btnEditRoutine);
                    setGone(holder.btnUndo);
                    holder.tvName.setTextColor(uncompleteColor);
                    holder.ivType.setColorFilter(dataItem.color);
                    if (!dataItem.isRepeat && DateUtil.isBeforeToday(dataItem.startTime)) {
                        SpannableString spannableString = new SpannableString(dataItem.name + "[已延期]");
                        spannableString.setSpan(new ForegroundColorSpan(Color.RED), dataItem.name.length(), spannableString.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                        holder.tvName.setText(spannableString);
                    } else {
                        holder.tvName.setText(dataItem.name);
                    }
                }
                if (dataItem.isRepeat) {
                    setGone(holder.btnArrange);
                }

            }

            @Override
            public int getItemViewType(int position) {
                return dataList.size() == position ? 1 : 0;
            }

            @Override
            public int getItemCount() {
                return dataList.size() + 1;
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                closeOpendItemIfExit();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                freshData();
            }
        }, 200);

    }


    public void freshData() {
        Calendar instance = Calendar.getInstance();
        RecordDao.getListOfDay(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH), instance.get(Calendar.DAY_OF_MONTH), new SqlCallback<List<EventRecord>>() {
            @Override
            public void onSucess(List<EventRecord> eventRecords) {
                recordsOfToday = eventRecords;
                ScheduleEventDao.getEventOfToday(new SqlCallback<List<ScheduleEvent>>() {
                    @Override
                    public void onSucess(List<ScheduleEvent> list) {
                        List<ScheduleEvent> toDoList = new ArrayList<>();
                        List<ScheduleEvent> doneList = new ArrayList<>();
                        for (ScheduleEvent scheduleEvent : list) {
                            if (isComplete(scheduleEvent)) {
                                doneList.add(scheduleEvent);
                            } else {
                                toDoList.add(scheduleEvent);
                            }
                        }
                        Collections.sort(toDoList, new Comparator<ScheduleEvent>() {
                            @Override
                            public int compare(ScheduleEvent lhs, ScheduleEvent rhs) {
                                return lhs.importance - rhs.importance;
                            }
                        });
                        dataList.clear();
                        dataList.addAll(toDoList);
                        dataList.addAll(doneList);
                        int timeSpent = 0;
                        for (ScheduleEvent scheduleEvent : dataList) {
                            timeSpent += scheduleEvent.timeSpent;
                        }
                        tvTimeSpent.setText(timeSpent <= 60 ? timeSpent + "min" : timeSpent / 60 + "h " + timeSpent % 60 + "min");
                        recyclerView.getAdapter().notifyDataSetChanged();
                        showOrHideCompleteInfo();
                    }
                });
            }
        });
    }

    private void updateStatisticsInfo() {
        int completeNum = 0;
        for (ScheduleEvent scheduleEvent : dataList) {
            if (isComplete(scheduleEvent)) completeNum++;
        }
        int uncompleteNum = dataList.size() - completeNum;
        tvNumAll.setText("" + dataList.size());
        tvNumUncomplete.setText("" + uncompleteNum);
        tvNumComplete.setText("" + completeNum);
    }

    private boolean isComplete(ScheduleEvent scheduleEvent) {
        if (scheduleEvent.isRepeat) {
            for (EventRecord eventRecord : recordsOfToday) {
                if (eventRecord.eventId == scheduleEvent.id) {
                    return true;
                }
            }
            return false;
        } else {
            return scheduleEvent.isComplete;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        closeOpendItemIfExit();
    }

    private void closeOpendItemIfExit() {
        if (null != slideListItemWrapper) {
            slideListItemWrapper.closeMenu();
            slideListItemWrapper = null;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            closeOpendItemIfExit();
        }
    }

    public void addHabbit(ScheduleEvent scheduleEvent) {
        recyclerView.scrollToPosition(0);
        dataList.add(0, scheduleEvent);
        recyclerView.getAdapter().notifyItemInserted(0);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.setToolbarColor(CompatUtil.getColor(mainActivity, R.color.colorPrimary));
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @Id(R.id.tv_name)
        TextView tvName;
        //        @Id(R.id.tv_sub)
//        TextView tvSub;
        @Id(R.id.btn_delete)
        View btnDelete;
        @Id(R.id.btn_undo)
        View btnUndo;
        @Id(R.id.first_show)
        View vFirstView;
        @Id(R.id.btn_alarm)
        View btnAlarm;
        @Id(R.id.btn_edit)
        View btnEditRoutine;
        @Id(R.id.btn_arrange)
        View btnArrange;
        @Id(R.id.iv_type)
        ImageView ivType;

        int type;


        public Holder(View itemView, int type) {
            super(itemView);
            this.type = type;
            if (type == 1) return;
            InjectionUtil.loadView(itemView, this);
            vFirstView.setOnClickListener(onItemClickListener);
            SlideListItemWrapper s = (SlideListItemWrapper) itemView.findViewById(R.id.slideListItemWrapper);
            s.setSlideListener(slideListener);
            btnDelete.setOnClickListener(onDeleteBtnClickListener);
            btnUndo.setOnClickListener(onUndoBtnClickListener);
            btnAlarm.setOnClickListener(onAlarmBtnClickListener);
            btnEditRoutine.setOnClickListener(onEditBtnClickListener);
            btnArrange.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null == satelliteMenu) {
                        satelliteMenu = new SatelliteMenu();
                    }
                    satelliteMenu.show(v, (ScheduleEvent) v.getTag());
                }
            });
        }

    }

    private final View.OnClickListener onAlarmBtnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            closeOpendItemIfExit();
            final ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            startActivity(PickTimeActivity.newIntent(getActivity(), new WatchTimePicker.OnTimeSelectListener() {
                @Override
                public void onTimeSelected(int hour, int minute) {
                    scheduleEvent.alarm = true;
                    scheduleEvent.alermHour = hour;
                    scheduleEvent.alermMinute = minute;
                    ScheduleEventDao.replace(scheduleEvent);
                    if (!scheduleEvent.isRepeat && DateUtil.getAlarmTime(scheduleEvent) < System.currentTimeMillis()) {
                        Snackbar.make(v, R.string.snack_time_passed, Snackbar.LENGTH_LONG).show();
                    } else {
                        AlarmReceiver.setAlarm(getContext().getApplicationContext(), scheduleEvent);
                        Snackbar.make(v, getResources().getString(R.string.snack_notify_time) + DateUtil.getAlarmTimeStr(scheduleEvent), Snackbar.LENGTH_LONG).show();
                    }
                }
            }));
        }
    };

    private void removeItemWithUiFresh(ScheduleEvent scheduleEvent) {
        int index = dataList.indexOf(scheduleEvent);
        dataList.remove(index);
        recyclerView.getAdapter().notifyItemRemoved(index);
        showOrHideCompleteInfo();
    }

    private final View.OnClickListener onItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            if (scheduleEvent.isRepeat) {
                startActivity(DetailActivity.newIntent(getActivity(), scheduleEvent, BaseActivity.fingerUpX, BaseActivity.fingerUpY));
                getActivity().overridePendingTransition(0, 0);
            } else {
                closeOpendItemIfExit();
                slideListItemWrapper = (SlideListItemWrapper) v.getParent();
                slideListItemWrapper.openMenu();
            }
        }
    };

    private final View.OnClickListener onDeleteBtnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeOpendItemIfExit();
            final ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            String toastStr = scheduleEvent.isRepeat ? "这将删除\"" + scheduleEvent.name + "\"的所有记录,确定?" : "确定要删除\"" + scheduleEvent.name + "\"吗";
            Snackbar snackbar = Snackbar.make(v, toastStr, Snackbar.LENGTH_LONG);
            snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ScheduleEventDao.delete(scheduleEvent);
                    removeItemWithUiFresh(scheduleEvent);
                    showOrHideCompleteInfo();
                }
            });
            snackbar.show();
        }
    };

    private final View.OnClickListener onUndoBtnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            if (!scheduleEvent.isRepeat) {
                scheduleEvent.isComplete = false;
                ScheduleEventDao.replace(scheduleEvent);
            } else {
                for (EventRecord eventRecord : recordsOfToday) {
                    if (eventRecord.eventId == scheduleEvent.id) {
                        RecordDao.delete(eventRecord);
                        recordsOfToday.remove(eventRecord);
                        break;
                    }
                }
            }
            closeOpendItemIfExit();
            removeItemWithUiFresh(scheduleEvent);
            dataList.add(0, scheduleEvent);
            recyclerView.getAdapter().notifyItemRangeInserted(0, 1);
            showOrHideCompleteInfo();
        }
    };

    private final View.OnClickListener onEditBtnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeOpendItemIfExit();
            ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            int[] loc = new int[2];
            v.getLocationInWindow(loc);
            startActivity(CreateOrModifyActivity.newIntent(getActivity(), scheduleEvent, loc[0] + v.getWidth() / 2, loc[1] + v.getHeight() / 2, scheduleEvent.color));
            getActivity().overridePendingTransition(0, 0);
        }
    };

    private final SlideListItemWrapper.ViewSlideListener slideListener = new SlideListItemWrapper.ViewSlideListener() {
        @Override
        public void onRightMenuSlideOut(SlideListItemWrapper v) {
            closeOpendItemIfExit();
            slideListItemWrapper = v;
        }

        @Override
        public void onLeftSlideTrigered(SlideListItemWrapper v) {
            closeOpendItemIfExit();
            ScheduleEvent scheduleEvent = (ScheduleEvent) v.getTag();
            if (scheduleEvent.isRepeat) {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                EventRecord eventRecord = new EventRecord(year, month, day, scheduleEvent.id);
                eventRecord.state = EventConstants.STATE_COMPLETE;
                RecordDao.replace(eventRecord);
                recordsOfToday.add(eventRecord);
            } else {
                scheduleEvent.isComplete = true;
                ScheduleEventDao.replace(scheduleEvent);
            }
            int index = dataList.indexOf(scheduleEvent);
            dataList.remove(index);
            dataList.add(scheduleEvent);
            recyclerView.getAdapter().notifyItemRemoved(index);
            recyclerView.getAdapter().notifyItemRangeInserted(dataList.size() - 1, 1);
            showOrHideCompleteInfo();
        }
    };


    public static ToDoListOfTodayFragment newInstance() {
        return new ToDoListOfTodayFragment();
    }


    class SatelliteMenu {
        private ScheduleEvent scheduleEvent;
        private final View.OnClickListener onClickTodayListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                satelliteView.collapse();
                scheduleEvent.startTime = DateUtil.getTodayStartTime();
                ScheduleEventDao.replace(scheduleEvent);
                recyclerView.getAdapter().notifyItemChanged(dataList.indexOf(scheduleEvent));
            }
        };
        private final View.OnClickListener onClickOnedayLaterListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeOpendItemIfExit();
                satelliteView.collapse();
                scheduleEvent.startTime = Long.MAX_VALUE;
                ScheduleEventDao.replace(scheduleEvent);
                removeItemWithUiFresh(scheduleEvent);
            }
        };
        SatelliteView satelliteView;
        private final View.OnClickListener onClickThisWeekListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                satelliteView.collapse();
                if (DateUtil.isTheLastDayOfWeek(DateUtil.getTodayStartTime())) {
                    Snackbar snackbar = Snackbar.make(v, R.string.snack_push_to_next_week, Snackbar.LENGTH_LONG);
                    snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            scheduleEvent.startTime = DateUtil.getTodayStartTime() + 3 * DateUtil.DAY_MILISECOND;
                            ScheduleEventDao.replace(scheduleEvent);
                            closeOpendItemIfExit();
                            removeItemWithUiFresh(scheduleEvent);
                        }
                    });
                    snackbar.show();
                } else {
                    scheduleEvent.startTime = DateUtil.getTimeLaterInThisWeek(DateUtil.getTodayStartTime());
                    ScheduleEventDao.replace(scheduleEvent);
                    closeOpendItemIfExit();
                    removeItemWithUiFresh(scheduleEvent);
                }
            }
        };
        private final View.OnClickListener onDelayToDayClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                satelliteView.collapse();
                startActivity(PickDateActivity.newIntent(getActivity(), new PickDateActivity.OnDateSelectListener() {
                    @Override
                    public void onDateSelect(int year, int month, int day) {
                        satelliteView.collapse();
                        if (DateUtil.isAfterToday(year, month, day)) {
                            scheduleEvent.startTime = DateUtil.getDayStartTime(year, month, day);
                            ScheduleEventDao.replace(scheduleEvent);
                            closeOpendItemIfExit();
                            removeItemWithUiFresh(scheduleEvent);
                        } else {
                            ToastUtil.showShortToast(getResources().getString(R.string.snack_push_to_today_or_future));
                        }
                    }
                }));
            }
        };


        private final View.OnClickListener onDelayToTheDayAfterTomorrowClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeOpendItemIfExit();
                satelliteView.collapse();
                scheduleEvent.startTime = DateUtil.getTodayStartTime() + 2 * DateUtil.DAY_MILISECOND;
                ScheduleEventDao.replace(scheduleEvent);
                removeItemWithUiFresh(scheduleEvent);
            }
        };
        private final View.OnClickListener onClickTororrowListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeOpendItemIfExit();
                satelliteView.collapse();
                scheduleEvent.startTime = DateUtil.getTodayStartTime() + DateUtil.DAY_MILISECOND;
                ScheduleEventDao.replace(scheduleEvent);
                removeItemWithUiFresh(scheduleEvent);
            }
        };
        private final View.OnClickListener onDelayInMonthClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                satelliteView.collapse();
                if (DateUtil.isTheLastDayOfMonth(DateUtil.getTodayStartTime())) {
                    Snackbar snackbar = Snackbar.make(v, R.string.snack_push_to_next_month, Snackbar.LENGTH_LONG);
                    snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            scheduleEvent.startTime = DateUtil.getTodayStartTime() + 7 * DateUtil.DAY_MILISECOND;
                            ScheduleEventDao.replace(scheduleEvent);
                            closeOpendItemIfExit();
                            removeItemWithUiFresh(scheduleEvent);
                        }
                    });
                    snackbar.show();
                } else {
                    scheduleEvent.startTime = DateUtil.getTimeLaterInThisMonth(DateUtil.getTodayStartTime());
                    ScheduleEventDao.replace(scheduleEvent);
                    closeOpendItemIfExit();
                    removeItemWithUiFresh(scheduleEvent);
                }
            }
        };

        private SatelliteMenu() {
            satelliteView = (SatelliteView) LayoutInflater.from(getContext()).inflate(R.layout.satellite_arrange_time_layout, null);
            satelliteView.findViewById(R.id.d_calendar).setOnClickListener(onDelayToDayClickListener);
            satelliteView.findViewById(R.id.d_day_after_tomorrow).setOnClickListener(onDelayToTheDayAfterTomorrowClickListener);
            satelliteView.findViewById(R.id.d_tomorrow).setOnClickListener(onClickTororrowListener);
            satelliteView.findViewById(R.id.d_this_month).setOnClickListener(onDelayInMonthClickListener);
            satelliteView.findViewById(R.id.d_this_week).setOnClickListener(onClickThisWeekListener);
            satelliteView.findViewById(R.id.d_today).setOnClickListener(onClickTodayListener);
            satelliteView.findViewById(R.id.d_oneday).setOnClickListener(onClickOnedayLaterListener);
        }

        public void show(View v, ScheduleEvent scheduleEvent) {
            this.scheduleEvent = scheduleEvent;
            int[] loc = new int[2];
            v.getLocationOnScreen(loc);
            int top = (int) SizeUtil.pxToDp(loc[1], getContext());
            int bottom = (int) SizeUtil.pxToDp(getResources().getDisplayMetrics().heightPixels - loc[1], getContext());
            int start = 180;
            int radius = 110;
            if (top < 140) {
                start += 60;
                radius += radius / 2;
            }
            int end = 360;
            if (bottom < 140) {
                end -= 60;
                radius += radius / 2;
            }
            satelliteView.expand(v, SizeUtil.dpToPx(radius, getContext()), start, end);
        }


    }

    private void showOrHideCompleteInfo() {
        tvComplete.setVisibility(isAllTaskCompleteToday() ? View.VISIBLE : View.GONE);
        updateStatisticsInfo();
    }

    private boolean isAllTaskCompleteToday() {
        for (ScheduleEvent scheduleEvent : dataList) {
            if (scheduleEvent.isRepeat) {
                if (!isComplete(scheduleEvent)) {
                    return false;
                }
            } else {
                if (!scheduleEvent.isComplete) {
                    return false;
                }
            }
        }
        return true;
    }

    @Click({R.id.num_complete, R.id.num_uncomplete, R.id.num_all})
    private void showCompleteSnack(View v) {
        int completeNum = 0;
        for (ScheduleEvent scheduleEvent : dataList) {
            if (isComplete(scheduleEvent)) completeNum++;
        }
        int uncompleteNum = dataList.size() - completeNum;
        String str;
        switch (v.getId()) {

            case R.id.num_complete:
                str = getResources().getString(R.string.snack_task_complete) + completeNum;
                break;
            case R.id.num_uncomplete:
                str = getResources().getString(R.string.snack_uncomplete_task) + uncompleteNum;
                break;
            default:
                str = getResources().getString(R.string.snack_all_task_of_today) + dataList.size();
        }
        Snackbar.make(v, str + getResources().getString(R.string.ge_unit), Snackbar.LENGTH_SHORT).show();
    }

    @Click(R.id.tv_time_spent)
    private void onTimeSpentClick(View v) {
        TextView textView = (TextView) v;
        Snackbar.make(v, getResources().getString(R.string.estimate_time_spent) + ((TextView) v).getText(), Snackbar.LENGTH_SHORT).show();
    }

}
